using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Services;
using MainGame.GamePlay;
using Microsoft.Xna.Framework.Input.Touch;

namespace MainGame.Menu
{

  public interface IHelpMenu
  {
    event MenuEvent OnHBackClicked;
  }

  public class HelpMenu : BaseMenu, IHelpMenu
  {

    Button helpBackBtn; //, TutorialBtn;
    Vector2 savedPos;
    public bool Animated = false;
    public int Direction = 1, AnimVelocity = 60;
    SpriteBatch spriteBatch;
    Color textColor;
    SpriteFont smallFont;
    Vector2 textPos, startingDragPos;
    string helpText;

    public HelpMenu(Game game, MenuDepths depth)
      : base(game, depth)
    {
      spriteBatch = Game.GetContext().SpriteBatch;
      background = game.Content.Load<Texture2D>(@"Images//Menu//HelpBg");
      bounds = new Rectangle(-game.GraphicsDevice.Viewport.Width, 0, game.GraphicsDevice.Viewport.Width, game.GraphicsDevice.Viewport.Height);
      game.Services.AddService(typeof(IHelpMenu), this);
      TouchPanel.EnabledGestures = GestureType.VerticalDrag | GestureType.DragComplete;
    }

    protected override void Dispose(bool disposing)
    {
      Game.Services.RemoveService(typeof(IHelpMenu));
      TouchPanel.EnabledGestures = GestureType.None;
      base.Dispose(disposing);
    }


    public override void Initialize()
    {
      savedPos.X = bounds.X;

      textPos = new Vector2(bounds.X, bounds.Y) + new Vector2(0, 30);
      helpText = Locale.Strings.HelpStr.Replace("\\n", "\n").Replace("\\t", "\t");
      startingDragPos = Vector2.Zero;
      // Load buttons
      helpBackBtn = new Button(Game, @"Images//Menu//button2", new Rectangle((int)bounds.X + 640, 370, 140, 55), Color.Blue, "small_font", Locale.Strings.Back, Color.White);
      AddButton(helpBackBtn, () =>
      {
        helpBackBtn.touched = true;
        Animated = true;
        Direction = -1;
      });

      textColor = new Color(0, 0, 120);

      smallFont = Game.Content.Load<SpriteFont>("small_font");

      base.Initialize();
    }


    public override void Update(GameTime gameTime)
    {
      // Input: back button pressed
      if (((Game1)Game).IsBackButtonPressed())
      {
        Animated = true;
        Direction = -1;
      }

      if (Animated)
      {
        // Animate the menu items to follow the menu sliding animation
        bounds.X += Direction * AnimVelocity;
        textPos.X += Direction * AnimVelocity;

        foreach (Button b in (from cb in buttons select cb.b))
          b.bounds.X += Direction * AnimVelocity;

        if (bounds.X > ((Game.GraphicsDevice.Viewport.Width - bounds.Width) / 2) && Direction == 1)
        {
          Animated = false;
          int offset = bounds.X;
          bounds.X = ((Game.GraphicsDevice.Viewport.Width - bounds.Width) / 2);

          foreach (Button b in (from cb in buttons select cb.b))
            b.bounds.X += -Direction * offset;

        }

        if (bounds.X < savedPos.X)
        {
          if (OnHBackClicked != null)
            OnHBackClicked();
        }
      }

      if (TouchPanel.IsGestureAvailable)
      {
        var g = TouchPanel.ReadGesture();

        switch (g.GestureType)
        {
          case GestureType.DragComplete:
            startingDragPos = Vector2.Zero;
            break;
          case GestureType.VerticalDrag:
            if (g.Delta == Vector2.Zero)
            {
              startingDragPos = g.Position;
            }
            else
            {
              var dragQty = Vector2.Subtract(g.Position, startingDragPos);
              //if (Math.Abs(dragQty.Y) > 1f)

              textPos.Y += dragQty.Y;
              startingDragPos = g.Position;

              if (textPos.Y <= -1000)
                textPos.Y = -1000;
              else if (textPos.Y >= 300)
                textPos.Y = 300;

            }
            break;
          default:
            break;
        }
      }

      base.Update(gameTime);
    }


    public override void Draw(GameTime gameTime)
    {
      base.Draw(gameTime);

      spriteBatch.Begin();

      spriteBatch.DrawString(smallFont, helpText, textPos + Vector2.One, Color.Black, 0.0f, Vector2.Zero, 0.8f, SpriteEffects.None, 0f);
      spriteBatch.DrawString(smallFont, helpText, textPos, Color.White, 0.0f, Vector2.Zero, 0.8f, SpriteEffects.None, 0f);

      spriteBatch.End();
    }

    public event MenuEvent OnHBackClicked;
  }
}
